package com.org.web.handle.product.controller;

import com.org.platform.model.ResultModel;
import com.org.platform.utils.CommonUtil;
import com.org.web.handle.product.service.ProductService;
import com.org.web.support.base.BaseController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

/**
 * @author devg @Date 2020/3/1 10:47
 */
@RestController
@RequestMapping(value = {"/product"})
public class ProductController extends BaseController {

  @Autowired
  private ProductService productService;

  @RequestMapping(value = {"/index"})
  public ModelAndView index(HttpServletRequest request) {
    String keyword = request.getParameter("keyword");
    String classifyId = request.getParameter("classifyId");
    Map<String, Object> param = new HashMap<>();
    param.put("keyword", keyword);
    param.put("classifyId", classifyId);
    return modelAndView("/front/productList", param);
  }

  @RequestMapping(value = {"getProdList"})
  public ResultModel getProdList(@RequestBody Map<String, Object> param) {
    String keyword = CommonUtil.getMapString(param, "keyword");
    String classifyId = CommonUtil.getMapString(param, "classifyId");
    if (CommonUtil.isNullOrEmpty(keyword) && CommonUtil.isNullOrEmpty(classifyId)) {
      return ResultModel.newFail("参数不能为空!");
    }
    List<Map<String, Object>> prodList = productService.getProdList(param);
    Map<String, Object> result = new HashMap<>();
    result.put("prodList", prodList);
    return ResultModel.newSuccess(result);
  }
}
